package com.baobaoyeye.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sun.io.Converters;

import com.baobaoyeye.conn.ConnMySQL;

/**
 * Servlet implementation class Addsubjects
 */
public class Addsubjects extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ConnMySQL connSql = null;   
    private ResultSet rs = null;
    private int TeacherId = -1;
    private int SubjectId = -1;
    private String _user_id;
    private String _type;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Addsubjects() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		_user_id = request.getParameter("_user_id");
		_type = request.getParameter("_type");
		String subname = request.getParameter("subname");
		String teachername = request.getParameter("teachername");
		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		connSql = new ConnMySQL();
		if(!IsHaveSub(subname) && IsHavaTeacher(teachername)){
			int ok = AddSubject(subname);
			if(ok==1){
				//out.println("add success");
				HttpSession session =request.getSession(true);
				if(_user_id!=null && _user_id.length()>0){
					session.setAttribute("_type",_type);
					session.setAttribute("_user_id",_user_id);
				}
				response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
				String newLocn = "Show/showsubjects.jsp";
				response.setHeader("Location", newLocn);
			}
			else{
				HttpSession session =request.getSession(true);
				if(_user_id!=null && _user_id.length()>0){
					session.setAttribute("_type",_type);
					session.setAttribute("_user_id",_user_id);
				}
				response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
				String newLocn = "addsubjects.jsp";
				response.setHeader("Location", newLocn);
				//out.println("DB error");
			}
		}
		else if(!IsHavaTeacher(teachername)){
			HttpSession session =request.getSession(true);
			if(_user_id!=null && _user_id.length()>0){
				session.setAttribute("_type",_type);
				session.setAttribute("_user_id",_user_id);
			}
			response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
			String newLocn = "addsubjects.jsp";
			response.setHeader("Location", newLocn);
			//out.println("mu you teacher");
		}
		
		
	}

	private int AddSubject(String subname) {
		String sql = "insert into subject(subject_id,subject_name,subject_teacher_id) value(null,'"+subname+"',"+TeacherId+");";
		int ok = 0;
		ok = connSql.executeUpdate(sql);
		return ok;
	}

	private boolean IsHavaTeacher(String teachername) {
		String sql = "select teacher_id from teacher where teacher_name='"+teachername+"';";
		rs = connSql.executeQuery(sql);
		boolean flag = false;
		try {
			if(rs.next()){
				String db_teacherid = rs.getString(1);
				
				TeacherId =  Integer.parseInt(db_teacherid);
				if(TeacherId != -1)
					flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.print("sdafasdfasf");
			e.printStackTrace();
		}
		try {
			connSql.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag == true)
			return true;
		else
			return false;
	}

	private boolean IsHaveSub(String subname) {
		String sql = "select subject_id from subject where subject_name='"+subname+"';";
		rs = connSql.executeQuery(sql);
		boolean flag = false;
		try {
			if(rs.next()){
				String db_subjectid = rs.getString(1);
				
				SubjectId  =  Integer.parseInt(db_subjectid);
				if(SubjectId != -1)
					flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			connSql.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag == true)
			return true;
		else
			return false;
	}

}
